package com.sqfw.project.wp.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqfw.common.exception.ServiceException;
import com.sqfw.common.utils.StringUtils;
import com.sqfw.project.wp.constant.DelEnum;
import com.sqfw.project.wp.domain.TSmsUser;
import com.sqfw.project.wp.mapper.TSmsUserMapper;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @description 针对表【T_SMS_USER】的数据库操作Service
* @createDate 2023-06-13 09:11:42
*/
@Service
public class TSmsUserService extends ServiceImpl<TSmsUserMapper, TSmsUser> implements IService<TSmsUser> {

    public LambdaQueryWrapper<TSmsUser> getListWrapper(TSmsUser tSmsUser) {
        LambdaQueryWrapper<TSmsUser> tSmsUserLambdaQueryWrapper = null;
        tSmsUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tSmsUserLambdaQueryWrapper
                .eq(TSmsUser::getIsDel, DelEnum.NODEL.getType()) // 删除标记(0-正常 1-删除)
                .eq(StringUtils.isNotEmpty(tSmsUser.getMobile()), TSmsUser::getMobile, tSmsUser.getMobile())
                .eq(StringUtils.isNotNull(tSmsUser.getGroupid()), TSmsUser::getGroupid, tSmsUser.getGroupid())
                .like(StringUtils.isNotEmpty(tSmsUser.getUsername()), TSmsUser::getUsername, tSmsUser.getUsername())
                .like(StringUtils.isNotEmpty(tSmsUser.getGroupname()), TSmsUser::getGroupname, tSmsUser.getGroupname());
        if(StringUtils.isNotNull(tSmsUser.getParams())
                && StringUtils.isNotNull(tSmsUser.getParams().get("beginTime"))
                && StringUtils.isNotNull(tSmsUser.getParams().get("endTime"))){
            tSmsUserLambdaQueryWrapper.apply("to_char(create_time, 'yyyy-mm-dd') BETWEEN '"
                    + tSmsUser.getParams().get("beginTime").toString() + "' and '"
                    + tSmsUser.getParams().get("endTime").toString() + "'");
        }
        return tSmsUserLambdaQueryWrapper;
    }

    public int saveSmsUser(TSmsUser tSmsUser){

        if(StringUtils.isEmpty(tSmsUser.getUsername())
                || StringUtils.isEmpty(tSmsUser.getMobile())
        ){
            throw new ServiceException("姓名和手机号不能为空");
        }

        if(!phoneCheck(tSmsUser.getMobile())){
            throw new ServiceException("手机号格式错误");
        }
        if (tSmsUser.getId()!=null){
            TSmsUser smsUser = this.getById(tSmsUser.getId());
            if (smsUser!=null&&StringUtils.isNotEmpty(smsUser.getMobile())&&!smsUser.getMobile().equals(tSmsUser.getMobile())){
                LambdaQueryWrapper<TSmsUser> tSmsUserLambdaQueryWrapper = new LambdaQueryWrapper<>();

                tSmsUserLambdaQueryWrapper.eq(TSmsUser::getMobile, tSmsUser.getMobile())
                        .eq(TSmsUser::getIsDel, DelEnum.NODEL.getType()); // 删除标记(0-正常 1-删除)
                long count = this.count(tSmsUserLambdaQueryWrapper);
                if(count>0){
                    throw new ServiceException("该手机号已存在");
                }
            }
        }else {
            LambdaQueryWrapper<TSmsUser> tSmsUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
            tSmsUserLambdaQueryWrapper.eq(TSmsUser::getMobile, tSmsUser.getMobile())
                    .eq(TSmsUser::getIsDel, DelEnum.NODEL.getType()); // 删除标记(0-正常 1-删除)
            long count = this.count(tSmsUserLambdaQueryWrapper);
            if(count>0){
                throw new ServiceException("该手机号已存在");
            }
        }

        return this.saveOrUpdate(tSmsUser)?1:0;
    }

    /**
     * 批量软删除
     * @param ids
     * @return
     */
    public int removeSmsUsers(String[] ids) {
        for (String id : ids) {
            TSmsUser tSmsUser = new TSmsUser();
            tSmsUser.setId(Long.parseLong(id));
            tSmsUser.setIsDel(DelEnum.DEL.getType()); // 删除标记(0-正常 1-删除)
            tSmsUser.setDeletetime(new Date()); // 删除时间
            this.updateById(tSmsUser);
        }
        return 1;
    }

    /**
     * 根据部门id查询用户（通讯录）手机号
     * @param groupid 部门id
     * */
    public List<TSmsUser> getSmsUserList (Long groupid) {
        LambdaQueryWrapper<TSmsUser> tSmsUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tSmsUserLambdaQueryWrapper.eq(TSmsUser::getGroupid, groupid)
                .eq(TSmsUser::getIsDel, DelEnum.NODEL.getType());
       return this.list(tSmsUserLambdaQueryWrapper);
    }


    private boolean phoneCheck(String mobile){
        String mobileRegEx = "^1[3,4,5,6,7,8,9][0-9]{9}$";//正则表达式
        Pattern pattern = Pattern.compile(mobileRegEx);//函数语法 匹配的正则表达式
        Matcher matcher = pattern.matcher(mobile);//进行匹配
        return matcher.matches();
    }

}
